package arrAndstr;

public class T134 {

    public static void main(String[] args) {
        int[] gas = new int[]{1,2,3,4,5};
        int[] cost = new int[]{3,4,5,1,2};

        System.out.println(canCompleteCircuit(gas, cost));;
    }

    public static int canCompleteCircuit(int[] gas, int[] cost) {
        int len = cost.length;

        for(int i=0; i<len; i++) {
            int gasNow = gas[i];
            int j = i;
            int step = 0;
            while(step < len) {
                if(cost[j%len] > gasNow) {
                    break;
                }
                gasNow -= cost[j%len];
                gasNow += gas[(++j)%len];
                step++;
            }
            if(step == len) {
                return i;
            }
        }
        return -1;
    }
}
